package model;

import java.util.ArrayList;

import controller.Controller;

public class Misc {
	private Controller controller = null;

	private String URL = null;
	private ArrayList<LabelText> labelList = null;

	public Misc(Controller controller) {
		this.controller = controller;

		this.labelList = new ArrayList<LabelText>();
		loadMiscFromDatabase();
	}

	private void loadMiscFromDatabase() {
		String[][] dbContent = controller.getSql().loadMiscFromDatabase();
		for (int i = 0; i < dbContent.length; i++) {
			String name = dbContent[i][0];
			String text = dbContent[i][1];
			if (name != "URL") {
				LabelText lt = new LabelText(name, text);
				labelList.add(lt);
			} else {
				this.URL = text;
			}
		}
	}

	public String getText(String name) {
		for (int i = 0; i < labelList.size(); i++) {
			LabelText current = labelList.get(i);
			if (current.getName().equals(name)) {
				return current.getText();
			}
		}
		return null;
	}

	public void changeText(String name, String newText) {
		for (int i = 0; i < labelList.size(); i++) {
			LabelText current = labelList.get(i);
			if (current.getName().equals(name)) {
				current.setText(newText);
				controller.getSql().updateMiscInDatabase(name, newText);
			}
		}
	}

	public String getURL() {
		return URL;
	}

	public void setURL(String URL) {
		this.URL = URL;
		controller.getSql().updateMiscInDatabase("URL", URL);
	}
	
	public ArrayList<LabelText> getLabelList(){
		return this.labelList;
	}

		
	
}
